package leetcode.每日一题;

import org.junit.Test;

/**
 * @author ：zsy
 * @date ：Created 2022/12/12 9:32 PM
 * @description：
 */
public class 所有子字符串美丽值之和 {
    @Test
    public void test() {

    }

    class Solution {
        public int beautySum(String s) {
            int res = 0;
            int len = s.length();
            for (int i = 0; i < len; i++) {
                int[] cnt = new int[26];
                int maxFreq = 0;
                for (int j = i; j < len; j++) {
                    cnt[s.charAt(j) - 'a']++;
                    maxFreq = Math.max(maxFreq, cnt[s.charAt(j) - 'a']);
                    int minFreq = s.length();
                    for (int k = 0; k < 26; k++) {
                        if (cnt[k] > 0) {
                            minFreq = Math.min(minFreq, cnt[k]);
                        }
                    }
                    res += maxFreq - minFreq;
                }
            }
            return res;
        }
    }
}
